<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Local</title>
    <style>
        html,body{height: 100%;}
    </style>
</head>

<body>

    <!-- AMD规范 -->
    <!-- <script data-main="amd/main" src="amd/amd.js"></script> -->

    <!-- CMD规范 -->
    <!-- <script src="cmd/cmd.js"></script>
    <script>
        seajs.use('./cmd/main.js');
    </script> -->


    <script>
        function debounce(fn, time) {
            let timer = null;
            return function (...args) {
                let ctx = this;
                clearTimeout(timer);
                timer = setTimeout(function () {
                    fn.apply(ctx, args);
                    timer = null;
                }, time);
            }
        }

        function testDebounce(event){
            console.log(this);
            console.log(event.type);
            console.log(`debounce ${new Date}`);
        }

        // document.body.addEventListener('mousemove', debounce(testDebounce, 300))

        // function throttle(fn, time){
        //     let timer = null;

        //     return function(...args){
        //         let ctx = this;
        //         if(!timer){
        //             timer = setTimeout(function(){
        //                 fn.apply(ctx, args);
        //                 timer = null;
        //             }, time);
        //         }
        //     }
        // }

        function throttle(fn, time) {
            let prev = +new Date;

            return function (...args) {
                let ctx = this;
                if (((+new Date) - prev) > time) {
                    fn.apply(ctx, args);
                    prev = +new Date;
                }
            }
        }
        function testThrottle(event){
            console.log(this);
            console.log(event.type);
            console.log(`throttle ${new Date}`);
        }

        document.addEventListener('mousemove', throttle(testThrottle, 1000).bind({event:"document"}))




        function asyncFn(result) {
            return Promise.resolve(result || new Date);
        }

        function handle(...args) {
            console.log(args);
        }

        // Promise.resolve(1).then(d=>{
        //     Promise.resolve(11);
        //     return 111;
        // }).then(handle);
        // Promise.resolve(1).then(d=>{
        //     return Promise.resolve(2);
        // }).then(handle);
    </script>
</body>

</html>